//
//  YunyingViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/10/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "YunyingViewController.h"



@interface YunyingViewController ()<UIWebViewDelegate,WKUIDelegate>

@end

@implementation YunyingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self loadWebView];
}

- (void)getData{


}

- (void)loadWebView{
    UIWebView *ZQXwebView = [[UIWebView alloc] init];
    ZQXwebView.delegate = self;
    ZQXwebView.frame = self.view.bounds;
    [self.view addSubview:ZQXwebView];
    
    NSString *pathString = [[NSBundle mainBundle] pathForResource:@"nvbar" ofType:@"html" inDirectory:@"operation"];
    [ZQXwebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pathString]]];

}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    // 以 html title 设置 导航栏 title
//    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//    // 关联 JSContext
//    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    // 打印异常
//    self.context.exceptionHandler =
//    ^(JSContext *context, JSValue *exceptionValue)
//    {
//        context.exception = exceptionValue;
//        NSLog(@"%@", exceptionValue);
//    };
//    // 装载数据
//    [self loadChartsData];
    
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *urlString = [[request URL]  absoluteString];
    if ([urlString containsString:@"js://"]) {
        NSLog(@"%@",urlString);
        [self sendDataToWebView:webView];
        
    }
    return YES;
}




#pragma mark - Load Charts Data

//传递数据给JS
- (void)sendDataToWebView:(UIWebView *)webView{

    NSArray *incomeData = @[@233, @241, @332,@234];
    NSDictionary *income = @{@"name": @"收入", @"data": incomeData};
    
    NSArray *payOutData = @[@238, @311, @321,@123];
    NSDictionary *payOut = @{@"name": @"支出", @"data": payOutData};
    
    NSArray *profitData = @[@323, @241, @323,@124];
    NSDictionary *profit = @{@"name": @"盈余", @"data": profitData};

    NSArray *seriesArray = @[income, payOut,profit];
    
    NSArray *xAxisArray = @[@"集散",@"包车",@"公交",@"出租车"];
    
    
    
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler =
        ^(JSContext *context, JSValue *exceptionValue)
        {
            context.exception = exceptionValue;
            NSLog(@"%@", exceptionValue);
        };
    [self.context[@"receiveData"] callWithArguments:@[seriesArray]];


}


- (void)loadChartsData
{
    // 装载数据
    NSArray *distributionData = @[@233, @241, @332];
    NSDictionary *distributionDict = @{@"name": @"集散", @"data": distributionData};
    
    NSArray *busData = @[@28, @311, @321];
    NSDictionary *busDict = @{@"name": @"公交", @"data": busData};
    
    NSArray *charterData = @[@33, @241, @32];
    NSDictionary *charterDict = @{@"name": @"包车", @"data": charterData};
    
    NSArray *taxiData = @[@8, @11, @21];
    NSDictionary *taxiDict = @{@"name": @"出租车", @"data": taxiData};
    
    NSArray *seriesArray = @[distributionDict, busDict,charterDict,taxiDict];
    
    [self.context[@"drawChart"] callWithArguments:@[seriesArray]];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/






@end
